Ext.define("byb.view.system.AdmOrg",{
    extend:"Ext.Panel",
    alias:"widget.sysmgr_orgnizeManager",
    border:false,
    layout:"border",
    ui: "byb",
    cls:"BaseManager",
    requires: [ 'byb.view.grid.system.AdmOrg','byb.view.system.AdmOrgTreeView'
                ,"byb.view.baseComponent.button.GridToolbarAllZK"
                ,"byb.view.baseComponent.button.GridToolbarAllSQ"],
    initComponent:function(){
        this.items=[
                    ,this.buildOrgTreeView()
                    ,this.buildOrgQueryView()
                    ];
        this.callParent(arguments);
    }
    ,buildOrgTreeView:function(){
    	var me = this;
        return {
        		xtype : 'admOrgTreeView'
        	,	width: 280
        	,	region: 'west'
        	,	dockedItems: [{
    	        xtype: 'toolbar',
    	        items: [{
    	            xtype: 'GridToolbarAllZK',
    	            handler: function(){
	            	 Ext.ComponentQuery.query('admOrgTreeView toolbar')[0].disable();
    	           	 Ext.ComponentQuery.query('admOrgTreeView')[0].expandAll(function(){
    	           		 Ext.ComponentQuery.query('admOrgTreeView toolbar')[0].enable();
    	           	 });
    	            }
    	        }, {
    	            xtype: 'GridToolbarAllSQ',
    	            handler: function(){
    	             if(Ext.ComponentQuery.query('admOrgTreeView')[0].getRootNode().getChildAt(0).isExpanded()){
		            	 Ext.ComponentQuery.query('admOrgTreeView toolbar')[0].disable();
	    	           	 Ext.ComponentQuery.query('admOrgTreeView')[0].collapseAll(function(){
	    	           		 Ext.ComponentQuery.query('admOrgTreeView toolbar')[0].enable();
	    	           	 });
    	             }
    	            }
    	        }]
    	    }]
        	, listeners:{
        		 itemcontextmenu : function(ctx, record, item, index, e, eOpts ){
        	    	e.preventDefault();  //屏蔽默认右键菜单            
        	    	var items = null;
        	    	var showDepartType = false;
        	    	var height = 83;
        	    	if(record.raw.data.code === 'BYB'){
        	    		height = 32;
        	    		items = [{
    		    	        text: '新增',
    		    	        iconCls:"icon-add"
    		    	    }];
        	    	}else{
        	    		items= [{
    		    	        text: '新增',
    		    	        iconCls:"icon-add"
    		    	    },{
    		    	        text: '修改',
    		    	        iconCls:"icon-edit"
    		    	    },{
    		    	        text: '删除',
    		    	        iconCls:"icon-del"
    		    	    }];
        	    	}
        	    	
        	    	var orgH = ctx.up('sysmgr_orgnizeManager').getEl().dom.scrollHeight;
        	    	var eY	 = e.getY();
        	    	var eX	 = e.getX();
        	    	if( (orgH - eY) < height){
        	    		eY = eY - height + 4;
        	    		eX = eX - 4;
        	    	}else{
        	    		eY = eY - 4;
        	    		eX = eX - 4;
        	    	}
        	    	
        	    	if(!Ext.ComponentQuery.query('admOrgTreeView')[0].up('admOrgTreeViewWindow')){
        	    		 Ext.create('Ext.menu.Menu', {
        		    	    width: 90,
        		    	    height: height,
        		    	    floating: false,  // usually you want this set to True (default)
        		    	    renderTo: Ext.getBody(),  // usually rendered by it's containing component
        		    	    items:items,
        		    	    listeners: {
        		    	        mouseleave : function(menu, e, eOpts ){
        		    	        	menu.destroy();
        		    	        }
        		    	        ,click:function(item, e, eOpts ){
        		    	        	if(e){
	        		    	        	if(e.text === '新增'){
	        		    	        		byb.controller.SystemController.action = null;
	        		    	        		byb.controller.SystemController.onAddOrg();
	        		    	        	}else if(e.text === '修改'){
	        		    	        		byb.controller.SystemController.action = 'modifyOrg';
	        		    	        		byb.controller.SystemController.onUpdateOrg();
	        		    	        	}else if(e.text === '删除'){
	        		    	        		byb.controller.SystemController.doDeleteOrg();
	        		    	        	}
        		    	        	}
        		    	        }
        		    	    }
        		    	}).showAt(eX, eY);
        	    	}
        	    }
        	}
        };
    },
    buildOrgQueryFormView:function(){
        return {
        	region:"north",
        	xtype:"form",
            border:false,
            layout:"hbox",
            defaultType:"textfield",
            height:40,
            labelWidth:90,
            items:[
           {
                fieldLabel:"组织名称",
	            labelStyle: "text-align:right;",
                style:"margin:10px 10px ",
                name:"name",
                allowBlank:true
            },{
            	xtype:'button',
                style:"margin:10px 10px ",
            	text:'查询'
            },{
                xtype:"button",
                style:"margin:10px 10px",
                text:"清除",
                action:"clear",
                handler:function(button){
                    var formPanel= button.up("form");
                    formPanel.getForm().reset();
                }
            }]	
        };
    },
    buildOrgGridView:function(){
    	return {
             region:"center",
    		 border:false,
       	     xtype:"sysmgr_orgnizeManagerGrid",
             bodyStyle:'background:white',
             frame:false,
             border:false,
             flex:1
       }
    },
    buildOrgQueryView:function(){
    	return {
    	 layout: 'border',
    	 region:'center',
		 border:false,
		 items:[
			this.buildOrgQueryFormView(), this.buildOrgGridView()
		   ]
    	}
    },
    listeners:{
	    afterrender: function() {
	        var store =  Ext.ComponentQuery.query('sysmgr_orgnizeManagerGrid')[0].getStore();
	        store.getProxy().setExtraParam("condition", '{}');
	        Ext.ComponentQuery.query('sysmgr_orgnizeManagerGrid pagingtoolbar')[0].moveFirst();
	    }
    }
});